package com.hdl.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * JedisPool连接池工具类
 */
public class JedisUtils {
    private static JedisPool jedisPool;

    static {
        //加载配置文件
        InputStream is = JedisUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
        System.out.println(is);

        //关联文件
        Properties pro = new Properties();
        try {
            pro.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //创建连接池配置对象
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(Integer.valueOf(pro.getProperty("maxIdle")));
        jedisPoolConfig.setMaxTotal(Integer.valueOf(pro.getProperty("maxTotal")));

        //初始化连接池对象
        jedisPool = new JedisPool(jedisPoolConfig,pro.getProperty("host"),Integer.valueOf(pro.getProperty("port")));

    }

    /**
     * 获取连接对象
     * @return Jedis连接对象
     */
    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
}
